package com.heima.corss.controller;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author jack
 * @data 2024 11:31
 */

public class TreeDemo {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        ts.add(new Student("张三",23));
        ts.add(new Student("李四",24));

        System.out.println(ts);

        System.out.println("-------------");

        TreeSet<String> ts1 = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        ts1.add("hello");
        ts1.add("java");
        ts1.add("world");
        ts1.add("hello");
        System.out.println(ts1);

    }
}

class Student /*implements Comparable<Student>*/{
    private String name;
    private int age;

  /*  @Override
    public int compareTo(Student o) {
        *//*
            this: 要添加的元素
            o:  集合中已有的
         *//*

        return o.age - this.age ;
    }*/

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
